//<!-- 解决登录页面嵌套问题 -->
if (window != top){
    top.location.href = location.href;
}
layui.use(['form','layer','jquery','element'],function(){
    var form = layui.form,
        layer = parent.layer === undefined ? layui.layer : top.layer
        $ = layui.jquery,
        element = layui.element
        mobileCapcha_btn=0;

    $(".loginBody .seraph").click(function(){
        layer.msg("这只是做个样式，至于功能，你见过哪个后台能这样登录的？还是老老实实的找管理员去注册吧",{
            time:5000
        });
    })

    //登录按钮
    form.on("submit(login)",function(data){
        let {username,password,captcha}=data.field;
        $.ajax({
            type:'post',
            url:'/login',
            data:{username,password,captcha:captcha.toLowerCase()},
            success:function ({msg,code,data,href}) {
                code==200?(()=>{
                    window.location.href=href;
                })():code==5001?(()=>{
                    alert(msg,code)
                })():code==5002?(()=>{
                    alert(msg,code)
                })():"";
            }
        })
        return false;
    })

    //点击 手机验证登录
    form.on("submit(mobilelogin)",function(data){
        let {mobile,mobileCapcha}=data.field;
        //if == 0 点击登录按钮 执行，if == 1 点击 获取验证码
        mobileCapcha_btn==0?(function () {
            //发起登录 手机短信登录认证
                $.ajax({
                    type:'post',
                    url:'/mobilelogin',
                    data:{mobile,captcha:mobileCapcha},
                    success:function ({msg,code,data,href}) {
                        code==200?(()=>{
                            console.log(msg,code,data,href)
                            window.location.href=href;
                        })():code==5001?(()=>{
                            alert(msg,code)
                        })():code==5002?(()=>{
                            alert(msg,code)
                        })():"";
                    }
                })
            })():(function () {
            //获取 根据 手机号 获取 验证码请求
            $.ajax({
                type:'post',
                url:'/mobileCapcha/getMobileCapcha',
                contentType:'application/json;charset=utf-8',
                data:JSON.stringify({mobile}),
                success:function ({msg,code,mobilecode}) {
                    code==200?(()=>{
                        console.log(msg,code,mobilecode)
                        alert(mobilecode)
                    })():code==2001?(()=>{
                        alert(msg,code)
                    })():code==5002?(()=>{
                        alert(msg,code)
                    })():"";
                }
            })
            mobileCapcha_btn=0;
            $("input[name=mobileCapcha]").attr("lay-verify",'required')
        })();
        return false;
    })

    //点击 获取验证码 按钮
   $("#getmobileCapcha").click(function (event) {
        mobileCapcha_btn=1;
        $("input[name=mobileCapcha]").attr("lay-verify",'')
        $(".submit").click()
        event.stopPropagation();
    })

    //tab 监听函数
    element.on('tab(docDemoTabBrief)',function (data) {
      /*  console.log(this); //当前Tab标题所在的原始DOM元素
        console.log(data.index); //得到当前Tab的所在下标
        console.log(data.elem); //得到当前的Tab大容器*/
    })

    //表单输入效果
    $(".loginBody .input-item").click(function(e){
        e.stopPropagation();
        $(this).addClass("layui-input-focus").find(".layui-input").focus();
    })
    $(".loginBody .layui-form-item .layui-input").focus(function(){
        $(this).parent().addClass("layui-input-focus");
    })
    $(".loginBody .layui-form-item .layui-input").blur(function(){
        $(this).parent().removeClass("layui-input-focus");
        if($(this).val() != ''){
            $(this).parent().addClass("layui-input-active");
        }else{
            $(this).parent().removeClass("layui-input-active");
        }
    })

    //点击 图片 验证码 刷新函数
    $("#img-code").click(function (event) {
        $(this).attr("src","/login/captcha?"+Math.random()*100)
        event.stopPropagation();
    })

    //弹窗提示统一函数
    function alert(msg,code) {
        layer.confirm(msg, {
            id:"erro",
            icon: 5,
            title:'提示',
            btn: ['确定'], //可以无限个按钮
            success:function(layero){
                //获取焦点
                var btn = layero[0].getElementsByClassName('layui-layer-btn')
                    [0].getElementsByTagName('A')[0];
                btn.href = 'javascript:void(0)';
                btn.focus();
            },
            btn1: function(index, layero){
                layer.close(index);
            },
            end:function(){
                let index = parent.layer.getFrameIndex(window.name); //先得到当前iframe层的索引
                parent.layer.close(index); //再执行关闭
                code==5002?$("input[name=captcha]").focus():$("input[name=password]").focus();
            }
        });
    }

})
